package Client;

import java.util.ArrayList;
import java.util.StringTokenizer;
import java.net.Socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import socket.SocketUtil;

/**
 * This class extends NetworkClient class
 * additionally handle the connection made 
 * @author Group 6
 */
public class NetworkClientTest extends NetworkClient {
	private ArrayList<LogLine> viewList;
	private LogLine logLine;

	/**
	 * LogLine arrayList: Contains the logFiles each line as an element
	 * LogLine: A temporary logLine object used to add logLine list 
	 * @param host of the current connection
	 * @param port of the current connection
	 */
	public NetworkClientTest(String host, int port) {
		super(host, port);
		viewList = new ArrayList<LogLine>();
		logLine = new LogLine();
	}

	/**
	 * A name string taken from textfield, output and input streams are created
	 * name string is sent to server and name's are returned with value matchings
	 * These name value pairs used to create logLine and added to logLine list
	 * @param client represents the socket
	 */
	protected void handleConnection(Socket client) throws IOException {
		//This name string taken from textfield
		String name = getLogLineName();
		//Output stream is created
		PrintWriter out = SocketUtil.getWriter(client);
		//name string is sent to server
		out.println(name);

		//The matched values taken from server with using BufferedReader
		BufferedReader in = SocketUtil.getReader(client);
		int size = 0;
		//Size of the message
		size = Integer.parseInt(in.readLine());
		//Clearing the list
		viewList.clear();
		LogLine newLogLine;
		//A new list is created which has the name value pairs
		for (int i = 0; i < size; i++) {
			newLogLine = new LogLine();
			//Stream is taken from server as a single string
			//And this string is splitted by string tokenizer
			StringTokenizer token = new StringTokenizer(in.readLine(), ",");
			//Each token used for create a new log line
			while (token.hasMoreTokens()) {
				newLogLine.setName(token.nextToken());
				newLogLine.setValue(Integer.parseInt(token.nextToken()));
			}
			//At last log lines added to new log line list
			//which have the name's value
			viewList.add(newLogLine);
		}
		//Closing the reader and writer
		in.close();
		out.close();
	}

	/**
	 * @return the logLineName
	 */
	public String getLogLineName() {
		return logLine.getName();
	}

	/**
	 * @param lgLineName
	 *            the logLine to set
	 */
	public void setLogLineName(String lgLineName) {
		this.logLine.setName(lgLineName);
	}

	/**
	 * @return the viewList
	 */
	public ArrayList<LogLine> getViewList() {
		return viewList;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "NetworkClientTest [viewList=" + viewList + ", logLine="
				+ logLine + "]";
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((logLine == null) ? 0 : logLine.hashCode());
		result = prime * result
				+ ((viewList == null) ? 0 : viewList.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (!(obj instanceof NetworkClientTest))
			return false;
		NetworkClientTest other = (NetworkClientTest) obj;
		if (logLine == null) {
			if (other.logLine != null)
				return false;
		} else if (!logLine.equals(other.logLine))
			return false;
		if (viewList == null) {
			if (other.viewList != null)
				return false;
		} else if (!viewList.equals(other.viewList))
			return false;
		return true;
	}

}
